package com.sunrise.gateway.commons;

import com.google.common.base.Charsets;
import com.google.common.collect.Sets;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Set;

/**
 * @author wangmin
 * @discription
 * @created 2020-03-10 14:49
 */
public abstract class MyClassPathResourceReader {
    public static Set<String> read(String classPath) throws IOException {
        Set<String> set = Sets.newHashSet();

        ClassPathResource pathResource = new ClassPathResource(classPath);
        InputStream inputStream = null;
        BufferedReader br = null;
        try {
            inputStream = pathResource.getInputStream();
            br = new BufferedReader(new InputStreamReader(inputStream, Charsets.UTF_8));
            String lineTxt;
            while ((lineTxt = br.readLine()) != null) {
                lineTxt = lineTxt.trim();
                if (!lineTxt.isEmpty()) {
                    set.add(lineTxt);
                }
            }
        } finally {
            if (br != null) {
                br.close();
            } else {
                if (inputStream != null) {
                    inputStream.close();
                }
            }
        }

        return set;
    }
}
